using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using NCT.Framework;
using NCT.Framework.Database;

namespace VISS.API.Admins {
	public class AdminBC {

        public static bool Insert(AdminEnt item) {
            bool result = true;
            try {

                if (item.DateCreated <= DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }

                //Encrypt password
                item.Password = StringUtils.DoMD5(item.Password);

                //Normalize data
                if (item.DateCreated <= DateTimeUtils.MinValue) {
                    item.DateCreated = DateTime.Now;
                }
                item.Name = StringUtils.Normalize(item.Name);

                // Add User to DB
                result = AdminDA.Insert(item, null, null) > 0;

                //Add log
                if (result) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Admin.AdminBC.Insert({0}, {1}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            return result;
        }

        public static bool UpdatePassword(AdminEnt item) {
            bool result = true;
            try {

                //Encrypt password
                item.Password = StringUtils.DoMD5(item.Password);

                //Normalize data
                if (item.DateModified <= DateTimeUtils.MinValue) {
                    item.DateModified = DateTime.Now;
                }

                // Update User to DB
                result = AdminDA.UpdatePassword(item, null, null) > 0;

                //Add log
                if (result) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Admin.AdminBC.UpdatePassword({0}, {1}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            return result;
        }

        public static bool UpdateRights(AdminEnt item) {
            bool result = true;
            try {

                //Normalize data
                if (item.DateModified <= DateTimeUtils.MinValue) {
                    item.DateModified = DateTime.Now;
                }

                // Update User to DB
                result = AdminDA.UpdateRights(item, null, null) > 0;

                //Add log
                if (result) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Admin.AdminBC.UpdateRights({0}, {1}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            return result;
        }

        public static bool UpdateType(AdminEnt item) {
            bool result = true;
            try {

                //Encrypt password
                item.Password = StringUtils.DoMD5(item.Password);

                //Normalize data
                if (item.DateModified <= DateTimeUtils.MinValue) {
                    item.DateModified = DateTime.Now;
                }

                // Update User to DB
                result = AdminDA.UpdateType(item, null, null) > 0;

                //Add log
                if (result) {
                }
            }
            catch (Exception ex) {
                LogUtils.Error(string.Format("VISS.Admin.AdminBC.UpdateType({0}, {1}): \n" + ex.ToString(),
                                    SerializerUtils.JsonSerialize(item)));
                result = false;
            }
            return result;
        }

        public static bool Login(string userName, string password, out AdminEnt item) {
            bool result = true;
            item = null;
            try
            {
                password = StringUtils.DoMD5(password);

                item = AdminDA.Login(userName, password, null, null);
                if (item == null) {
                    result = false;
                }
                else {
                    item.LastLogin = DateTime.Now;
                    AdminDA.UpdateLastLogin(item, null, null);
                }
            }
            catch (System.Exception ex)
            {
                LogUtils.Error(string.Format("VISS.Admin.AdminBC.Login({0}, {1}): \n" + ex.ToString(),
                                    userName,
                                    password));
                result = false;
            }
            return result;
        }
	}
}